package com.mud.mudcommune.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by welcome to china on 2016/12/10.
 */

public class MyIndexBar extends View {
    private float cellheight;
    private float cellwidth;
    private Paint paint;
    float x=0,y=0;
    public MyindexOnclickListener myindexOnclickListener;
    private String[] words={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T",
            "U","V","W","X","Y","Z"};

    public String[] getWords() {
        return words;
    }

    public MyIndexBar(Context context) {
        this(context,null);
    }

    public void setAnyway(MyIndexBar.MyindexOnclickListener myindexOnclickListener) {
        this.myindexOnclickListener = myindexOnclickListener;
    }

    public MyIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    private void initData() {
        paint=new Paint();
        paint.setTextSize(40);//设置字体大小
        paint.setAntiAlias(true);//设置抗锯齿
        paint.setColor(Color.WHITE);//设置字体的颜色为白色
        paint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体粗体


    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(cellwidth==0)cellwidth=getMeasuredWidth();
        if(cellheight==0)cellheight=getMeasuredHeight()/26;
        for (int i = 0; i < words.length; i++) {
            String word=words[i];
            x=cellwidth/2-paint.measureText(word)/2;//设置每次控件的宽度
            Rect rect=new Rect();
            paint.getTextBounds(word,0,word.length(),rect);//所得的高度会放矩形里面
            y=i*cellheight+cellheight/2+rect.height()/2;//拿到了每次控件的一个高度
            paint.setColor(Color.parseColor(lastindex==i?"#666666":"#ffffff"));//每次设置控件的颜色  刚好绘制和点击的一致
            canvas.drawText(word,x,y,paint);
        }
    }
     int lastindex=-1;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index= (int) (event.getY()/cellheight);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                if(lastindex!=index&&index<words.length){
                    lastindex=index;
                    String word=words[index];
                   if(myindexOnclickListener!=null){
                       myindexOnclickListener.taoge(word);
                   }
                }
                break;
            case MotionEvent.ACTION_UP:
                lastindex=-1;
                break;
        }
        invalidate();//刷新数据的意思
        return true;
    }
    public interface MyindexOnclickListener{
        void taoge(String words);
    }
}
